/**
 * 
 */
package com.suyx.job.utils.mh;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.suyx.job.config.BaiduOCR;
import com.suyx.job.service.mh.impl.MhxyServiceImpl;
import com.suyx.job.utils.DeviceCodeMapUtil;
import com.suyx.job.utils.ImagePHash;
import com.suyx.job.utils.ImageUtils;
import com.suyx.job.utils.SampleOCRUtil;
import com.suyx.job.utils.ScreenCapture;

import lombok.extern.slf4j.Slf4j;

/**
 * @author syx
 *
 */
@Slf4j
@Component
public class WaTuUtils {
	@Autowired
    private BaiduOCR baiduOCR;
	
	private static String SC_PIC_PATH = "H:\\android\\MH\\";//梦幻宝图截图放置路径
	
	public  void startWatu(String device,String zuobiao,String baoguo) throws Exception{
        String path = SC_PIC_PATH + device +"\\";
        while(true){
        	ScreenCapture.snapshot(device,path, "screen.png");
            ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
                    new FileOutputStream(path + device +".png"), 93,18,117,35);
            
            ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
                    new FileOutputStream(path + device +"_.png"), 120,48,63,39);
            
            Thread.sleep(3 * 1000L);
            ScreenCapture.snapshot(device,path, "screen.png");
            ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
                    new FileOutputStream(path + device +"_1.png"), 120,48,63,39);
            ImagePHash p = new ImagePHash();
            String image1 = p.getHash(new FileInputStream(new File(path + device + "_.png")));
            String image2 = p.getHash(new FileInputStream(new File(path + device + "_1.png")));
            int score0 =  p.distance(image1, image2);
            log.info("识别"+DeviceCodeMapUtil.deviceCode_map.get(device)+"号机器是否到达宝图坐标点："+score0);
        	if(score0 == 0){
        		ScreenCapture.mouseClick(device, "1308 675");//  # 打开包裹
        		ScreenCapture.mouseClick(device, baoguo) ;//点击藏宝图包裹
        		ScreenCapture.mouseClick(device, "522 422");//#使用
        		ScreenCapture.mouseClick(device,"1156 52");//#关闭包裹
        		//Thread.sleep(2000l);
        		ScreenCapture.snapshot(device,path, "screen.png");
               
                ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
                        new FileOutputStream(path + device + "_zd.png"), 862,635,88,88);

                image1 = p.getHash(new FileInputStream(new File(path + device + "_zd.png")));
                image2 = p.getHash(new FileInputStream(new File(SC_PIC_PATH +"zd.png")));
                int score =  p.distance(image1, image2);
                if(score < 10){
        			ScreenCapture.mouseClick(device, "1390 681");// #自动
        			while(true){
        				Thread.sleep(5 * 1000L);
        				ScreenCapture.snapshot(device,path, "screen.png");
        				ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
        		                new FileOutputStream(path + device + "_zd.png"), 901,660,30,30);
        		        image1 = p.getHash(new FileInputStream(new File(path + device + "_zd.png")));
        		        image2 = p.getHash(new FileInputStream(new File(SC_PIC_PATH +"zd_over.png")));
        		        int score_over =  p.distance(image1, image2);
        		        if(score_over < 10){
        		        	ScreenCapture.mouseClick(device, "1242 558");//  # 取消自动自动
        		        	break;
        		        }
        			}
        			
        		}
                //# 加血
    			ScreenCapture.mouseClick(device, "1400 40");//  
    			ScreenCapture.mouseClick(device, "1193 109");//  # 加血
    			ScreenCapture.mouseClick(device, "1400 40");//
    			ScreenCapture.mouseClick(device, "1359 109");//  # 使用
    			ScreenCapture.mouseClick(device, "1258 33");// 
    			ScreenCapture.mouseClick(device, "992 103");//  # 使用
                break;
        	}
        }
	}
}
